#include "OverlappedData.h"
#include "TCP/TCPClient.h"


#define DEBUG_CREATE_DESTROY_COUNT		1

#if DEBUG_CREATE_DESTROY_COUNT
extern uint32_t CreateRef = 0;
extern uint32_t DestroyRef = 0;
#endif

OverlappedData* OverlappedData_Alloc(int type)
{
#if DEBUG_CREATE_DESTROY_COUNT
	InterlockedIncrement(&CreateRef);
#endif
	OverlappedData* data = new OverlappedData();
	memset(data, 0, sizeof(OverlappedData));
	data->Buffer.buf = data->Data;
	data->Buffer.len = BUFFER_SIZE;
	data->AddressLen = sizeof(sockaddr);
	data->Type = type;
	return data;
}

void OverlappedData_Free(OverlappedData** data)
{
	if (data && *data)
	{
#if DEBUG_CREATE_DESTROY_COUNT
		InterlockedIncrement(&DestroyRef);
#endif
		if ((*data)->Client)
		{
			if ((*data)->Type == OVERLAPPED_TYPE_RECV)
				delete (*data)->Client;
			else
				throw;
		}
		delete (*data);
		*data = nullptr;
	}
}

